package TestPatience;

##########################
#     OmnibusProxy       #
#    Krzysztof Ulman     #
# Politechnika Krakowska #
#      Kraków 2012       #
##########################

use strict;
use warnings;
use encoding 'utf8';
use utf8;

use ContentManager;
use Spider;

#maksymalna liczba wynikow
our $MAX_RESULTS = 50;

#robot sieciowy
my $spider;

#adres strony glownej wyszukiwarki
my $searcherUrl;

#lista zapytan
my @statements;

#konstruktor
#do testowania wyszukiwarek
sub new {
	shift;
	my $self = {};
	bless $self;
	
	my $searcherUrl = shift;
	$self->{searcherUrl} = $searcherUrl;
	
	private_prepareSpider($self);
	private_prepareStatements($self);
	
	return $self;
	
}

#przygotowanie robota do pracy
sub private_prepareSpider {
	my$self = shift;
	my$searcherUrl = $self->{searcherUrl};
	my $spider = Spider->new();
	$spider->configure($searcherUrl);
	$self->{spider} = $spider;
}

#przygotowanie listy fraz do zapytania
sub private_prepareStatements {
	my $self = shift;
	my @statements;

	my $fileName = 'statements_small.txt';
	#pobranie danych z pliku	
	open(INPUT_FILE, $fileName) || die("Brak pliku " . $fileName);
	while(my $line = <INPUT_FILE>) {
		chomp $line;
    	push(@statements, $line);
 	}
	close INPUT_FILE;
	
	$self->{statements} = \@statements;
}

#uruchomienie testu
sub runTest {
	my $self = shift;
	my $spider = $self->{spider};

	my @statements = @{$self->{statements}};
	my $statementsNumber = @statements;
	my $currentStatement = 0;
	
	my $continueTest = 1;
	my $phaseNum = 0;
	open(WRITER, ">writer.txt");
	while($continueTest) {
		$phaseNum++;
		
		# 1) wybranie frazy
		my $statement = $statements[$currentStatement];
		$currentStatement++;
		if($currentStatement >= $statementsNumber) {
			$currentStatement = 0;
		}
		println("\nWybrana fraza (nr $phaseNum): " . $statement);
		print WRITER "\nWybrana fraza (nr $phaseNum): " . $statement . "\n";

		
		# 2) wykonanie pierwszego zapytania o dana fraze
		my $resultNum = 1;
		my $content = $spider->doQuery($statement);
		my $contentManager = ContentManager->new($content);
		my @resultList = @{$contentManager->getResultList()};
		foreach my $result (@resultList) {
			println($resultNum . ") " . $result->getUrl());
			print WRITER $resultNum . ") " . $result->getUrl() . "\n";
			$resultNum++;
		}
		
		# 3) wykonanie zapytan o nastepne strony
		my $nextPageUrl = $contentManager->getNextPageUrl();
		while($nextPageUrl ne '' && $resultNum < $MAX_RESULTS) {
			$content = $spider->doGet($nextPageUrl);
			$contentManager = ContentManager->new($content);
			@resultList = @{$contentManager->getResultList()};
			println(">>");
			print WRITER ">>\n"; 
			foreach my $result (@resultList) {
				println($resultNum . ") " . $result->getUrl());
				print WRITER $resultNum . ") " . $result->getUrl() . "\n";
				$resultNum++;
			}
		}
		
		# 4) wypisanie informacji
		println("\nAktualnie pobranych stron: " . $spider->getTotalPageCounter());
		print WRITER "\nAktualnie pobranych stron: " . $spider->getTotalPageCounter() . "\n";
	}
	close WRITER;
}

sub println {
	my $text = shift;
	print "$text\n";
}

1;